package com.example.tkserver.service.impl;

import com.example.tkserver.dao.AnswerDao;
import com.example.tkserver.entity.Answer;
import com.example.tkserver.service.interfaces.AnswerService;
import com.example.tkserver.entity.MyException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log4j2
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    AnswerDao answerDao;

    @Override
    public int getAnswerNum(int qid) {
        log.info("获取回答数量:"+qid);
        int num = answerDao.getAnswerNum(qid);
        log.info("获取回答数量完成:"+num);
        return num;
    }

    @Override
    public List<Answer> getAnswersBetween(int qid, int start, int end) {
        log.info("获取回答从"+start+"到"+end+":"+qid);
        List<Answer> answers = answerDao.getAnswerBetween(qid,start,end);
        log.info("获取回答完成:"+answers);
        return answers;
    }

    @Override
    public Answer pushAnswer(Answer answer) {
        log.info("发布回答:"+answer);
        answer.setDatetime(System.currentTimeMillis() / 1000);
        if (answerDao.publishAnswer(answer) == -1){
            throw new MyException("发布回答失败，请联系管理员进行反馈！");
        }
        log.info("发布回答完成:"+answer);
        return answer;
    }
}
